package com.gdhz.xiaohengqin.device.service.impl;

import com.gdhz.xiaohengqin.device.model.entity.AccessRecord;
import com.gdhz.xiaohengqin.device.repository.AccessRecordRepository;
import com.gdhz.xiaohengqin.device.service.AccessRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Mono;

/**
 * @author Swallows
 * @date 2025-06-04 10:54
 */
@Slf4j
@Service
public class AccessRecordServiceImpl implements AccessRecordService {

    private final AccessRecordRepository repository;

    public AccessRecordServiceImpl(AccessRecordRepository repository) {
        this.repository = repository;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Mono<Void> saveRecord(AccessRecord record) {
        log.debug(" 正在保存记录: {}", record);
        return repository.save(record).then()
                .doOnSuccess(__ -> log.info(" 保存成功"))
                .onErrorResume(e -> {
                    log.error(" 保存失败", e);
                    return Mono.empty();   // 或自定义恢复逻辑
                });
    }
}
